<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单编号" prop="orderId">
      <!-- <el-input v-model="dataForm.orderId" placeholder="订单编号"></el-input> -->
      <template>
        <el-select v-model="dataForm.orderId" @click.native="getOrders()" placeholder="请选择">
          <el-option
            v-for="item in Orders"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="收货地址" prop="receiveAddressId">
      <!-- <el-input v-model="dataForm.receiveAddressId" placeholder="收货地址"></el-input> -->
      <template>
        <el-select v-model="dataForm.addressId" @click.native="getAddress()" placeholder="请选择">
          <el-option
            v-for="item in Addresses"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="订单备注" prop="orderComment">
      <el-input v-model="dataForm.orderComment" placeholder="订单备注"></el-input>
    </el-form-item>
    <el-form-item label="付款方式" prop="paymentWay">
      <!-- <el-input v-model="dataForm.paymentWay" placeholder="付款方式【 1:在线付款 2:货到付款】"></el-input> -->
      <el-radio-group v-model="dataForm.paymentWay">
        <el-radio :label="1">在线付款</el-radio>
        <el-radio :label="2">货到付款</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="任务状态" prop="taskStatus">
      <!-- <el-input v-model="dataForm.taskStatus" placeholder="任务状态"></el-input> -->
      <el-steps :space="200" :active="dataForm.taskStatus" :process-status="dataForm.taskStatus == 0?'finish ':'process'" finish-status="success">
        <el-step :title="dataForm.taskStatus == 0?'退货':'已付款'" :icon="dataForm.taskStatus == 0?'el-icon-tickets':'el-icon-edit'"></el-step>
        <el-step :title="dataForm.taskStatus == 1?'待出库':'已出库'" :icon="dataForm.taskStatus == 1?'el-icon-loading':'el-icon-edit'"></el-step>
        <el-step :title="dataForm.taskStatus == 2?'待收货':'已收货'" :icon="dataForm.taskStatus == 2?'el-icon-loading':'el-icon-edit'"></el-step>
        <el-step title="完成" :icon="dataForm.taskStatus == 3?'el-icon-loading':'el-icon-edit'"></el-step>
      </el-steps>
      <el-button style="margin-top: 12px;" v-if="dataForm.taskStatus<4" :disabled="dataForm.taskStatus==0?true:false" @click="next">下一步</el-button>
      <el-button style="margin-top: 12px;" v-else @click="next" type="warning">退货</el-button>
    </el-form-item>
    <el-form-item label="订单描述" prop="orderBody">
      <el-input type="textarea" v-model="dataForm.orderBody" placeholder="订单描述"></el-input>
    </el-form-item>
    <el-form-item label="物流单号" prop="trackingNo">
      <el-input v-model="dataForm.trackingNo" placeholder="物流单号"></el-input>
    </el-form-item>
    <el-form-item label="生成时间" prop="createTime">
      <!-- <el-input v-model="dataForm.createTime" placeholder="生成时间"></el-input> -->
      <div class="block">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="仓库id" prop="wareId">
      <!-- <el-input v-model="dataForm.wareId" placeholder="仓库id"></el-input> -->
      <template>
        <el-select v-model="dataForm.wareId" placeholder="请选择" @click.native="getWare()">
          <el-option
            v-for="item in Wares"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="工作单备注" prop="taskComment">
      <el-input type="textarea" v-model="dataForm.taskComment" placeholder="工作单备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        orders: [],
        wares: [],
        WareSkus: [],
        receiveAddresses: [],
        wareTaskAll: [],
        dataForm: {
          id: 0,
          orderId: '',
          receiveAddressId: '',
          orderComment: '',
          paymentWay: 1,
          taskStatus: 1,
          orderBody: '',
          trackingNo: '',
          createTime: '',
          wareId: '',
          taskComment: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          receiveAddressId: [
            { required: true, message: '收货地址不能为空', trigger: 'blur' },
            { validator: function (rule, value, back) {
              if (this.dataForm.orderId === null) {
                back(new Error('请选择订单'))
              } else {
                back()
              }
            },
              trigger: 'blur'
            }
          ],
          orderComment: [
            { required: true, message: '订单备注不能为空', trigger: 'blur' }
          ],
          paymentWay: [
            { required: true, message: '付款方式不能为空', trigger: 'blur' }
          ],
          taskStatus: [
            { required: true, message: '任务状态不能为空', trigger: 'blur' }
          ],
          orderBody: [
            { required: true, message: '订单描述不能为空', trigger: 'blur' }
          ],
          trackingNo: [
            { required: true, message: '物流单号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '生成时间不能为空', trigger: 'blur' }
          ],
          wareId: [
            { required: true, message: '仓库id不能为空', trigger: 'blur' }
          ],
          taskComment: [
            { required: true, message: '工作单备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      Orders: function () {
        let orderIds = []
        this.wareTaskAll.forEach(waretask => {
          orderIds.push(waretask.order.id)
        })
        if (this.dataForm.id) {
          return this.orders
        } else {
          return this.orders.filter(order => !orderIds.includes(order.id))
        }
      },
      Addresses: function () {
        let userid = this.wareTaskAll.filter(ware => ware.order.id === this.dataForm.orderId).order.userId
        return this.receiveAddresses.filter(address => address.userId === userid)
      },
      Wares: function () {
        let skuId = this.wareTaskAll.filter(ware => ware.order.id === this.dataForm.orderId).order.skuId
        let wareIds = []
        this.WareSkus.filter(waresku => waresku.sku.id === skuId).forEach(waresku => {
          wareIds.push(waresku.ware.id)
        })
        return this.wares.filter(ware => wareIds.includes(ware.id))
      }
    },
    methods: {
      next () {
        if (this.dataForm.taskStatus > 3) {
          this.dataForm.taskStatus = 0
          return
        }
        this.dataForm.taskStatus = this.dataForm.taskStatus + 1
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ware/ware_order_task/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.wareOrderVo.id
                this.dataForm.orderId = data.wareOrderVo.order.id
                this.orders = data.wareOrderVo.order
                this.dataForm.receiveAddressId = data.wareOrderVo.receiveAddress.id
                this.receiveAddresses = data.wareOrderVo.receiveAddress
                this.dataForm.orderComment = data.wareOrderVo.orderComment
                this.dataForm.paymentWay = data.wareOrderVo.paymentWay
                this.dataForm.taskStatus = data.wareOrderVo.taskStatus
                this.dataForm.orderBody = data.wareOrderVo.orderBody
                this.dataForm.trackingNo = data.wareOrderVo.trackingNo
                this.dataForm.createTime = data.wareOrderVo.createTime
                this.dataForm.wareId = data.wareOrderVo.ware.id
                this.wares = data.wareOrderVo.ware
                this.dataForm.taskComment = data.wareOrderVo.taskComment
              }
            })
          }
          this.wareTaskAll = []
          this.$http({
            url: this.$http.adornUrl(`/ware/ware_order_task/list`),
            method: 'get',
            params: this.$http.adornParams({
              'page': 1,
              'limit': 10000,
              'key': null
            })
          }).then((data) => {
            this.wareTaskAll = data.data.page.list
          })
          this.getWareSku()
        })
      },
      getOrders () {
        this.orders = []
        this.$http({
          url: this.$http.adornUrl(`/order/order/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            'key': null
          })
        }).then((data) => {
          this.orders = data.data.page.list
        })
      },
      getAddress () {
        this.receiveAddresses = []
        this.$http({
          url: this.$http.adornUrl(`/user/receive_address/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            'key': this.dataForm.order.userId
          })
        }).then((data) => {
          this.receiveAddresses = data.data.page.list
        })
      },
      getWare () {
        this.wares = []
        this.$http({
          url: this.$http.adornUrl(`/ware/ware/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            'key': this.dataForm.order.skuId
          })
        }).then((data) => {
          this.wares = data.data.page.list
        })
      },
      getWareSku () {
        this.WareSkus = []
        this.$http({
          url: this.$http.adornUrl(`/ware/ware_sku/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            'key': null
          })
        }).then((data) => {
          this.WareSkus = data.data.page.list
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ware/ware_order_task/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.order.id,
                'receiveAddressId': this.dataForm.receiveAddressId,
                'orderComment': this.dataForm.orderComment,
                'paymentWay': this.dataForm.paymentWay,
                'taskStatus': this.dataForm.taskStatus,
                'orderBody': this.dataForm.orderBody,
                'trackingNo': this.dataForm.trackingNo,
                'createTime': this.dataForm.createTime,
                'wareId': this.dataForm.wareId,
                'taskComment': this.dataForm.taskComment
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
